#-*- encoding: utf-8 -*-
import locale
import poplib
from email import parser
import email
import base64
import string

import sys
reload(sys)
sys.setdefaultencoding('utf8')

import os
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'TuringClass.settings')
import django
from app_TuringClass.models import *
from app_OnlineExam.models import *
import datetime
import django.utils.timezone as timezone

# 确定运行环境的encoding
__g_codeset = sys.getdefaultencoding()
if "ascii"==__g_codeset:
    __g_codeset = locale.getdefaultlocale()[1]
#

def object2double(obj):
    if(obj==None or obj==""):
        return 0
    else:
        return float(obj)
    #end if
#

def utf8_to_mbs(s):
    return s.decode("utf-8").encode(__g_codeset)
#

def mbs_to_utf8(s):
    return s.decode(__g_codeset).encode("utf-8")
#

def addAnswers(QuestionID, AnswerContent, UserEmail):
    try:
        user = TuringUser.objects.get(UserEmail = UserEmail)
    except Exception as e:
        print e
        return False
    UserID =  user.UserID
    AnswerID = time.strftime("%Y%m%d%H%M%S", time.gmtime()) + '_' + UserID
    try:
        QuestionAnswers(
            AnswerContent = AnswerContent,
            QuestionID = QuestionID,
            AnswerID = AnswerID,
            ResponderID = UserID
            ResponderName = user.NickName).save()
    except Exception as e:
        print e
        return False
    else:
        return True



host = 'pop.163.com'
username = 'turingclass@163.com'
password = 'qwerty123'

# host = 'pop.exmail.qq.com'
# username = '3345947441@qq.com'
# password = 'qwerty123'

pop_conn = poplib.POP3_SSL(host)
pop_conn.user(username)
pop_conn.pass_(password)

#Get messages from server:
# 获得邮件
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
#print messages

#print "--------------------------------------------------"
# Concat message pieces:
messages = ["\n".join(mssg[1]) for mssg in messages]
#print messages

#Parse message intom an email object:
# 分析
messages = [parser.Parser().parsestr(mssg) for mssg in messages]

suc = 0
fail = 0

for index in range(0,len(messages)):
    message = messages[index];
    subject = message.get('subject')
    h = email.Header.Header(subject)
    dh = email.Header.decode_header(h)
    if dh[0][1] == None:
        subject = dh[0][0]
    else:
        subject = unicode(dh[0][0], dh[0][1]).encode('utf8')
    #mailName = "mail%d.%s" % (i, subject)
    #f = open('%d.log'%(i), 'w');
    #print >> f, "Date: ", message["Date"]
    #print >> f, "From: ", email.utils.parseaddr(message.get('from'))[1]
    #print >> f, "To: ", email.utils.parseaddr(message.get('to'))[1]
    #print >> f, "Subject: ", subject
    info = subject.split('_')
    if info[0] != 'Re:TuringClass':
        pop_conn.dele(index + 1)
    for part in message.walk():
        fileName = part.get_filename()
        contentType = part.get_content_type()
        mycode=part.get_content_charset();
        # 保存附件
        if fileName:
            data = part.get_payload(decode = True)
            h = email.Header.Header(fileName)
            dh = email.Header.decode_header(h)
            fname = dh[0][0]
            encodeStr = dh[0][1]
            if encodeStr != None:
                fname = fname.decode(encodeStr, mycode)
            #end if
            fEx = open("%s"%(fname), 'wb')
            fEx.write(data)
            fEx.close()
        elif contentType == 'text/plain':# or contentType == 'text/html':
            #保存正文
            t = part.get_content_charset()
            #data = part.get_payload(decode = 'base64')
            if t == None:
                data = part.get_payload()
            else:
                data = unicode(part.get_payload(decode = 'base64'), t)
            if addAnswers(info[1], data, email.utils.parseaddr(message.get('from'))[1]):
                pop_conn.dele(index + 1)
                suc += 1
            else:
                fail += 1
                print subject + ': ' + failed
            #print >> f, data
        #end if
    #end for
    #f.close()
#end for
pop_conn.quit()
